<?php
	


class OCharset extends LibBase{
	
	private $opp = array(
						'1'=>'UTF-8',
						'2'=>'GB18030',
						'3'=>'GBK',
						'4'=>'GB2312'
						);

	public function __construct(){}
	
	public function get($id){
		if(!isset($this->opp[$id])){
			throw new PException("找不到编码");
		}
		return $this->opp[$id];
	}
	
	public function getIdByStr($code){
		$code = strtoupper($code);
		$id = array_search($code, $this->opp);
		
		
		//linruiyan@2012-12-1 如果未正确指定编码，那么应该抛出异常。不能自动指定默认编码
		if(false===$id){
			throw new PException("未支持该编码:{$code}");
		}
		return $id; 
		
	}
	

	/**
	 * 将内容转换为UTF-8的内容
	 */	
	public function iconv2UTF8($code,$content){
		$id = $this->getIdByStr($code);
		
		return 1==$id?$content:iconv($this->opp[$id],'UTF-8//IGNORE',$content);//IGNORE参数可以忽略转换中的字符错误
	}

}
